//
//  MYGHomePageSearchVC.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/23.
//

import UIKit
import JXSegmentedView

class MYGHomePageSearchVC: MYGBaseController  {

    var type = 6
    lazy var segment:JXSegmentedView = {
         let control  = JXSegmentedView()
         control.delegate = self
         return control
     }()
    
    lazy var searchTextField: UITextField = {
        let control = UITextField.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: navHeight))
        control.textAlignment = .center
        control.placeholder = "搜索"
        control.clearButtonMode = .whileEditing
        control.backgroundColor = UIColor.init(hexString: "f5f5f5")
        control.layer.cornerRadius = navHeight/2
        control.layer.masksToBounds = true
        control.addTarget(self, action: #selector(textFieldChanged(_:)), for: .editingChanged)
        control.returnKeyType = .search
        return control
    }()
    @objc func textFieldChanged(_ textField: UITextField){
        if let text = textField.text {
            NotificationCenter.default.post(name: Notification.Name("MYGCollectListViewController_refreshData"), object: text)
        }
    }
     var segmentedDataSource: JXSegmentedTitleDataSource = {
         let control = JXSegmentedTitleDataSource()
         control.titles = [ "新闻", "话题"]
         control.isTitleColorGradientEnabled = true
         return control
     }()
     override func viewDidLoad() {
         super.viewDidLoad()
         configSegMent()
     }
     
     func configSegMent() {
         
         self.navigationItem.titleView = self.searchTextField
         
         self.view.addSubview(segment)
         segment.snp.makeConstraints { make in
             make.top.left.right.equalToSuperview()
             make.height.equalTo(46.5)
         }
         //关联dataSource
         segment.dataSource = self.segmentedDataSource
         
         let indicator = JXSegmentedIndicatorLineView()
         indicator.indicatorWidth = 20
         segment.indicators = [indicator]
         
         
         let listContainerView = JXSegmentedListContainerView(dataSource: self)
         view.addSubview(listContainerView)
         listContainerView.snp.makeConstraints { make in
             make.top.equalTo(segment.snp.bottom).offset(15)
             make.left.right.bottom.equalToSuperview()
         }
         //关联listContainer
         segment.listContainer = listContainerView
     }
 }
 extension MYGHomePageSearchVC: JXSegmentedViewDelegate {
     func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
         
     }
 }


 extension MYGHomePageSearchVC : JXSegmentedListContainerViewDataSource {
     //返回列表的数量
     func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
         return segmentedDataSource.titles.count
     }
     
     //返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例
     func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
         let vc =  MYGCollectListViewController()
         vc.fromMain = true
         vc.type = exchangeType(index: index)
         return vc
     }
     
     func exchangeType(index:Int) ->Int {
        
         if index == 0 {
             self.type = 6//新闻
         }
         if index == 1 {
             self.type = 1//话题
         }
        
         return self.type
     }

}
